package com.lanp.springes.controller;

import com.lanp.springes.model.es.ProductEs;
import com.lanp.springes.model.es.SkuEs;
import com.lanp.springes.repository.ProductEsRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/test")
public class TestRest {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private ProductEsRepository productEsRepository;

    @PostMapping("createIndex")
    public Object createIndex() {
        return elasticsearchRestTemplate.createIndex(ProductEs.class);
    }

    @PostMapping("/save")
    public Object save(@RequestBody ProductEs productEs) {
        return productEsRepository.save(productEs);
    }

    @PostMapping("/saveAll")
    public Object saveAll(@RequestBody ProductEs productEs, Integer size) {
        List<ProductEs> list = new ArrayList<>();
        list.add(productEs);
        Calendar calendar = Calendar.getInstance();
        Long timeInMillis = calendar.getTimeInMillis() / 1000000;
        Random random = new Random();

        for (int i = 1; i < size; i++) {
            ProductEs newProductEs = new ProductEs();
            int i1 = random.nextInt(1000000);
            newProductEs.setProductId(timeInMillis.intValue() + i1);
            newProductEs.setProductName("商品" + newProductEs.getProductId());
            newProductEs.setCateId(productEs.getCateId());
            newProductEs.setCreateTime(new Date());
            newProductEs.setPrice(productEs.getPrice());
            newProductEs.setSkus(new ArrayList<>());
            SkuEs skuEs = new SkuEs();
            skuEs.setSkuId(timeInMillis.intValue() + random.nextInt(1000000));
            skuEs.setSkuName("sku" + skuEs.getSkuId());
            skuEs.setProductId(newProductEs.getProductId());
            skuEs.setCateId(newProductEs.getCateId());
            skuEs.setPrice(newProductEs.getPrice());
            newProductEs.getSkus().add(skuEs);
            list.add(newProductEs);
        }
        for (ProductEs es : list) {
            productEsRepository.save(es);
        }
        return "🆗";
    }

    @GetMapping("/get")
    public Object get(ProductEs productEs) {
        return productEsRepository.findById(productEs.getProductId());
    }

    @GetMapping("/findAll")
    public Object findAll() {
        return productEsRepository.findAll();
    }

    @GetMapping("/findByProductName")
    public Object findByProductName(ProductEs productEs) {
        Pageable pageable = Pageable.unpaged();
        return productEsRepository.findByProductName(productEs.getProductName(), pageable);
    }

    @GetMapping("/find")
    public Object find(ProductEs productEs) {

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();

        TermsAggregationBuilder price_Agg = AggregationBuilders.terms("price")
                .field("price").size(1000);
        queryBuilder.addAggregation(price_Agg);

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("cateId", 1));
        queryBuilder.withQuery(boolQueryBuilder);

        Pageable pageable = PageRequest.of(0, 5);
        queryBuilder.withPageable(pageable);
        return elasticsearchRestTemplate.queryForPage(queryBuilder.build(), ProductEs.class);
    }

}
